﻿//-----------------------------------------------------------------------
// <copyright file="ConnectionSelector.cs" company="MSCRMTools">
//     Copyright © MSCRMTools. All rights reserved.
// </copyright>
// <author>tanguy92</author>
// <date>21/02/2011</date>
// <codeplexurl>http://searchablepropertyupdater.codeplex.com</codeplexurl>
// <summary>no summary</summary>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CrmConnectionProject
{
    /// <summary>
    /// Formulaire de sélection d'une connexion à un serveur
    /// Crm dans une liste de connexions existantes.
    /// </summary>
    public partial class ConnectionSelector : Form
    {
        #region Variables

        /// <summary>
        /// Connexion sélectionnée
        /// </summary>
        ConnectionDetail selectedConnection;

        /// <summary>
        /// Obtient la connexion sélectionnée
        /// </summary>
        public ConnectionDetail SelectedConnection
        {
            get { return selectedConnection; }
        }

        private bool HadCreatedNewConnection = false;

        #endregion

        #region Constructeur

        /// <summary>
        /// Créé une nouvelle instance de la classe ConnectionSelector
        /// </summary>
        /// <param name="connections">Liste des connexions disponibles</param>
        public ConnectionSelector(CrmConnections connections)
        {
            InitializeComponent();

            connections.Connections.Sort();

            foreach (ConnectionDetail detail in connections.Connections)
            {
                ListViewItem item = new ListViewItem(detail.ConnectionName);
                item.SubItems.Add(detail.Organization);
                item.SubItems.Add(detail.ServerName);
                item.Tag = detail;

                this.lvConnections.Items.Add(item);
            }
        }

        #endregion

        #region Properties

        public bool HadCreatedNewConnection1
        {
            get { return HadCreatedNewConnection; }
        }

        #endregion

        #region Méthodes

        private void btnNewConnection_Click(object sender, EventArgs e)
        {
            ConnectionForm cForm = new ConnectionForm(true);
            cForm.StartPosition = FormStartPosition.CenterParent;

            if (cForm.ShowDialog(this) == DialogResult.OK)
            {
                this.selectedConnection = cForm.CrmConnectionDetail;
                this.HadCreatedNewConnection = true;

                ListViewItem item = new ListViewItem(this.selectedConnection.ConnectionName);
                item.SubItems.Add(this.selectedConnection.ServerName);
                item.SubItems.Add(this.selectedConnection.Organization);
                item.Tag = this.selectedConnection;

                this.lvConnections.Items.Add(item);
                this.lvConnections.SelectedItems.Clear();
                item.Selected = true;

                this.bValidate_Click(sender, e);
            }
            else
            {
                //this.bCancel_Click(sender, e);
                //return;
            }
        }

        private void lvConnections_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.bValidate_Click(sender, e);
        }
        
        private void bValidate_Click(object sender, EventArgs e)
        {
            if (this.lvConnections.SelectedItems.Count > 0)
            {
                this.selectedConnection = this.lvConnections.SelectedItems[0].Tag as ConnectionDetail;

                if (string.IsNullOrEmpty(this.selectedConnection.UserPassword) && (this.selectedConnection.UseIFD || this.selectedConnection.UseOnline || this.selectedConnection.IsCustomAuth))
                {
                    PasswordForm pForm = new PasswordForm() 
                    {
                        UserDomain = this.selectedConnection.UserDomain,
                        UserLogin = this.selectedConnection.UserName,
                        StartPosition = FormStartPosition.CenterParent
                    };

                    if (pForm.ShowDialog() == DialogResult.OK)
                    {
                        this.selectedConnection.UserPassword = pForm.UserPassword;
                    }
                    else
                    {
                        return;
                    }
                }

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void bCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        #endregion

       
    }
}